/******************************************************************************************
Use Case name and Id: 2.0 Post Group Ride Event 
by Michael Puzon,  lastupdated March 20, 2010
Class: COIN 71

Functional Specification

When the post button is clicked, checkdata function is called. This function checks to see whether important data is filled up and not empty. Upon clearing checkdata, the page issues a post request using form data to process_postride.php, which issues back response in json format. The response is either a failure, with an error message, or a success with a json of data that was submitted successfully.

Web Master Maintenance:
******************************************************************************************
Web master may update a different php servlet by modifying postData() function, and changing the process_postride.php parameter.

*/

//outputs content in the output div
//element- the kind of element, usually p for paragraph
//content- the text of the content
function outputDiv(element, content){
	var output = document.getElementById("output");
	addNode(output, element, "", content);	
}

//outputs content with a link in the output div
//parcontent- the text of the content
//linkcontent- the text of the link content
//href- the address of the link
function outputLink(href, content){
	var output = document.getElementById("output");
	var parnode = addNode(output, "p", "", "");
	addLink(parnode, href, content);
}

//parses the json response after submitting data to process_postride.php
//checks against null response, and unsuccessful responses
//upon successful response, outputs response in output Div, and takes away the form
//for submission
function parseResponse(response){
	if (response == null){
		outputDiv("p", "Error Encountered: Could not connect to server at this time");
		return;
	}
	if (!response.success){
		outputDiv("p", "Error Encountered: " + response.message);
	} else{ 
		var title = document.getElementById("title");
		outputDiv("h2", title.value + ' event successfully added to Calendar!');
		outputDiv("h3", "Event Details :");
		for (x in response.fields)  {		
			outputDiv("p", response.fields[x].type + " : " + 
			newLine(response.fields[x].detail));
		}
		outputLink("calendar.php", "Link to Calendar Page");
		$("#stylized").remove();
	}	
}

//checks each required data field if filled up and not empty
//returns false if a data field is empty, and true otherwise
function checkData(){
	var datafields = new Array();
	var title = document.getElementById("title");
	datafields.push(title);
	var rideDate = document.getElementById("date");
	if (!(checkDate(rideDate.value))){
		outputDiv("p", "date is in an invalid format");
		return false;
	}
	datafields.push(rideDate);	
	var rideTime = document.getElementById("time");
	if (!(checkTime(rideTime.value))){
		outputDiv("p", "time is in an invalid format");
		return false;
	}
	datafields.push(rideTime);
	var location = document.getElementById("location");
	datafields.push(location);
	var rating = document.getElementById("rating");
	datafields.push(rating);
	var leadername = document.getElementById("leadername");
	datafields.push(leadername);
	var contact = document.getElementById("contact");
	datafields.push(contact);
	
	for (x in datafields)  {
		if (datafields[x].value == ""){
			outputDiv("p", datafields[x].id + " is empty, Please fill info");
			return false;
		}
	}	
		
	return true;
}

//checks date using regular expression in mm/dd/yyyy format
//returns true if valid and false otherwise
function checkDate(datestr){
	var patt = new RegExp("^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$", 'i');	
	var match = datestr.match(patt);
	if (match == null)
		return false;
	return true;
}

//checks time using regular expression in hh:mm (am|pm) format
//returns true if valid and false otherwise
function checkTime(timestr){
	var patt = /^(1[0-2]|0[1-9]):([0-5][0-9]) (am|pm)$/;
	var match = timestr.match(patt);
	if (match == null)
		return false;
	return true;
}

//uses jquery ajax post function to process_viewride.php
//data is taken from form elements in the page
//if php servlet encounters unspecified error, response in html is called up in outputDiv
//calls up parseResponse function if response is in json format
function postData(){
	$("#output").empty();
	if (checkData()){
		var form =  document.getElementById("rideform");
		$.post("process_postride.php",
		$("#rideform").serialize(),
		function(data){
			data = $.trim(data);
			if (data.charAt(0) == "{")
				parseResponse(JSON.parse(data));
			else
				outputDiv("p", data);
 		});		
	}
	else outputDiv("p", "form was not submitted");
}

//assign the proper onclick event to Post Ride button
function setupBtn(){
	var postbtn = document.getElementById("submitbtn");	
	postbtn.setAttribute("onclick", "postData()");
}

//initialize datapicker and timepicker when page is initially loaded
//timepicker jquery is set to use 12 hr format
$(function() {
	$("#date").datepicker();
	$('#time').timepickr({convention: 12});
	setupBtn();	
});
